#include "utils.h"

double sslr(double s1, int a)
{
	int sign = (int) (s1 / abs(s1));
	s1 = abs(s1);
	int n;
	switch (a)
	{
		case 1:
			n = 10; break;
		case 2:
			n = 100; break;
		case 3:
			n = 1000; break;
		default:
			n = 1;
			break;
	}


	//设置要保留的位数
	double s2 = s1*n;               //将输入的数据乘以n，使得小数点后的第一位使我们要判断的是否为5的数
	double s3;
	char c1[20];
	char c2[20];


	sprintf_s(c1, "%lf", s2);  //将double类型的a2打印到字符数组s1里面去
	int b = strcspn(c1, ".");  //整型b即为小数点在字符数组中序号
	char c = c1[b + 1];      //字符c即为小数点后一位数


	if (c<'5')         //如果c小于5，则后面的舍去
	{
		strncpy_s(c2, c1, b);
		s3 = atof(c2);
	}
	else
	{
		if (c>'5')  //如果c大于5，则进1
		{
			strncpy_s(c2, c1, b);
			s3 = atof(c2) + 1;
		}
		else
		{
			//如果c大、等于5，则对前一位数进行判断奇偶
			c = c1[b - 1];
			if ((c % 2) != 0)  //C前一位为奇则进1
			{
				strncpy_s(c2, c1, b);
				s3 = atof(c2) + 1;
			}
			else
			{
				strncpy_s(c2, c1, b);
				s3 = atof(c2);
			}
		}


	}
	s3 = s3 / n;
	//cout << s3 << endl;  //输出结果
	return s3 * sign;
}

std::vector<string> getFiles(const fs::path& dir_path) {
	std::vector<string> file_paths;
	for (const auto &entry: fs::directory_iterator(dir_path)) { // 遍历目录
		if (fs::is_directory(entry)) continue;
		file_paths.push_back(entry.path().string()); // 添加文件路径
	}
	return file_paths;
}

short sgn(double x) {
	return short (x / abs(x));
}
